package cn.com.generaldata.jsme_indicator_system.vo.reportdesigner;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;

import lombok.Data;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;

/**
 * 报表设计器报表对象.
 */
@Data
public class ReportTemplateInfo {

	/** 报表类型id */
	private String reportTypeId;
	/** 报表标题 **/
	// private String reportTitle;
	/** 报表主列,多个主列用逗号分开 */
	private String masterColumn;

	/** 报表制度 */
	private int statSystem;

	/** 表内逻辑关系公式 */
	private String formulaString;

	/** 报表标题区域单元格对象,如有跨行跨列单元格时，会有重复的对象 */
	private List<ReportTitleCell> reportTitle = ImmutableList.of();

	/** 报表标题区域单元格对象,如有跨行跨列单元格时，会有重复的对象 */
	private List<ReportTitleCell> tableHead = ImmutableList.of();

	/** 报表表格主体区域单元格对象,如有跨行跨列单元格时，会有重复的对象 */
	private List<ReportBodyCell> tableBody = ImmutableList.of();

	/** 报表表格聚合行,如有跨行跨列单元格时，会有重复的对象 */
	private List<ReportBodyCell> tableBodyAgg = ImmutableList.of();

	/** 报表尾部区域单元格对象,如有跨行跨列单元格时，会有重复的对象 */
	private List<ReportFooterCell> reportFoot = ImmutableList.of();

	private ReportFormula reportFormula;

	public Set<ReportTitleCell> getUniqueReportTitles() {
		return Sets.newHashSet(getReportTitle());
	}

	public Set<ReportTitleCell> getUniqueReportTableHeads() {
		return Sets.newHashSet(getTableHead());
	}

	public Set<ReportBodyCell> getUniqueReportTableBodys() {
		return Sets.newHashSet(getTableBody());
	}

	public Set<ReportBodyCell> getUniqueReportTableBodyAggs() {
		return Sets.newHashSet(getTableBodyAgg());
	}

	public Set<ReportFooterCell> getUniqueReportFoots() {
		return Sets.newHashSet(getReportFoot());
	}

	public Set<ReportCell> getUniqueReportCells() {
		Set<ReportCell> cells = Sets.newHashSet();
		cells.addAll(getUniqueReportTitles());
		cells.addAll(getUniqueReportTableHeads());
		cells.addAll(getUniqueReportTableBodys());
		cells.addAll(getUniqueReportFoots());
		return cells;
	}

	/**
	 * 获取报表数据区域的起始行.
	 * 
	 * @return
	 */
	public int getReportTableBodyStartRow() {
		int rowIndex = 0;
		for (ReportTitleCell cell : getUniqueReportTableHeads()) {
			if (cell.getRowid() >= rowIndex) {
				rowIndex = cell.getRowid();
			}
		}
		return rowIndex + 1;
	}

	/** 获取单元格式报表数据区域终止行 */
	public int getCellReportTableBodyEndRow() {
		SortedSet<Integer> set = Sets.newTreeSet();
		for (ReportBodyCell cell : getUniqueReportTableBodys()) {
			set.add(cell.getRowid());
		}
		return set.last();
	}

	/** 获取单元格式报表底部区域起始行 */
	public int getCellReportFootStartRow() {
		SortedSet<Integer> set = Sets.newTreeSet();
		for (ReportFooterCell cell : getUniqueReportFoots()) {
			set.add(cell.getRowid());
		}
		return set.first();
	}

	/** 获取单元格式报表底部区域终止行 */
	public int getCellReportFootEndRow() {
		SortedSet<Integer> set = Sets.newTreeSet();
		for (ReportFooterCell cell : getUniqueReportFoots()) {
			set.add(cell.getRowid());
		}
		return set.last();
	}

	/**
	 * 获取报表尾部含有静态内容单元格的行号，如果没有则返回空
	 */
	public Integer getReportFootMinStaticRow() {
		SortedSet<Integer> set = Sets.newTreeSet();
		for (ReportFooterCell cell : getUniqueReportFoots()) {
			if (ReportCell.CELL_TYPE_STATIC.equals(cell.getType())) {
				set.add(cell.getRowid());
			}
		}
		return set.isEmpty() ? null : set.first();
	}

	/**
	 * 获取报表尾部含有静态内容单元格的行号，如果没有则返回空
	 */
	public Integer getReportFootMinRow() {
		SortedSet<Integer> set = Sets.newTreeSet();
		for (ReportFooterCell cell : getUniqueReportFoots()) {
			set.add(cell.getRowid());
		}
		return set.isEmpty() ? null : set.first();
	}

	/**
	 * 获取报表尾部区域的行数.
	 */
	public Integer getReportFootRowCount() {
		SortedSet<Integer> set = Sets.newTreeSet();
		for (ReportFooterCell cell : getUniqueReportFoots()) {
			set.add(cell.getRowid());
		}
		return set.last() - set.first() + 1;
	}

	public ReportFooterCell getReportFootMinStaticRowCell() {
		int minStaticRow = getReportFootMinStaticRow();
		for (ReportFooterCell cell : getUniqueReportFoots()) {
			if (cell.getRowid() == minStaticRow && ReportCell.CELL_TYPE_STATIC.equals(cell.getType())) {
				return cell;
			}
		}
		return null;
	}

	public Integer getTableBodyIndicatorId(int rowid, int colid) {
		Integer indicatorId = null;
		for (ReportBodyCell cell : tableBody) {
			if (cell.getRowid() == rowid && cell.getColid() == colid) {
				if (cell.getIndicatorid() != null) {
					indicatorId = cell.getIndicatorid();
				}
			}
		}
		return indicatorId;
	}

	/**
	 * 取出表尾的标识单元格.
	 * 
	 * @return
	 */
	public ReportFooterCell getReportFootMarkCell() {
		int minStaticRow = getReportFootMinStaticRow();
		for (ReportFooterCell cell : getUniqueReportFoots()) {
			if (cell.getRowid() == minStaticRow && ReportCell.CELL_TYPE_STATIC.equals(cell.getType())) {
				if (StringUtils.isNotBlank(cell.getContent())) {
					return cell;
				}

			}
		}
		return null;
	}

	public boolean isMasterColumn(int columnIndex) {
		return Arrays.asList(masterColumn.split(",")).contains(String.valueOf(columnIndex));
	}

}
